ABC279 D - Freefall
提出
TLE
code: python
import math
a, b = list(map(int, input().split()))
# 減少 -> 増加 or -> 増加
# B * n + A / √n+1 0 <= n <=
n = 1
res = a
while(True):
tmp = b * n + a / math.sqrt(n+1)
if (res < tmp):
print(res)
exit()
else:
res = tmp
n += 1
解答
code: python
import math
a, b = map(int, input().split())
n = math.floor((a / (2.0 * b)) ** (2.0 / 3.0)) - 1.0
if n < 0:
n = 0
# nは近似だから+1も考える
result = b * n + a / math.sqrt(n + 1.0)
result = min(result, b * (n + 1.0) + a / math.sqrt(n + 2.0))
print(result)
code: python
import math
a, b = map(int, input().split())
def f(n, a, b):
return a / math.sqrt(n + 1) + b * n
l, r = 0, pow(10, 18)
# ------------
# l m1 m2 r
while r - l > 2: # 3分割の幅
# ex) l = 0 r = 30:
# m1 = (0*2 + 30) // 3 = 30 // 3 = 10
# m2 = (0 + 30*2) // 3 = 60 // 3 = 20
m1 = (l*2 + r) // 3
m2 = (l + r*2) // 3
if f(m1, a, b) > f(m2, a, b):
l = m1
else:
r = m2
ans = min(f(l, a, b), f(l+1, a, b), f(r, a, b))
print(f"{ans:.10f}")
メモ
https://www.youtube.com/watch?v=BEkpK0z9yrE&t=2595s
提出
code: python
a, b = map(int, input().split())
# b * i + a / pow(i+1, 0.5)
# for i in range(10):
# print(b * i + a / pow(i+1, 0.5))